package com.horay.statemachiedemo.multi.action;

import com.horay.statemachiedemo.multi.constant.StateMachineConstant;
import com.horay.statemachiedemo.multi.event.ComplexEvent;
import com.horay.statemachiedemo.multi.model.Form;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;


/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@WithStateMachine(id = StateMachineConstant.COMPLEX_MACHINE_ID)
public class ComplexEventAction {

    /**
     * 当前状态BLANK_FORM
     */
    @OnTransition(target = "BLANK_FORM")
    public void create() {
        log.info("---空白复杂表单---");
    }

    @OnTransition(source = "BLANK_FORM", target = "FULL_FORM")
    public void write(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---填写完复杂表单---, form:[{}]", form);
    }

    @OnTransition(source = "FULL_FORM", target = "CHECK_CHOICE")
    public void check(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---校验复杂表单---, form:[{}]", form);
    }

    //不会执行
    @OnTransition(source = "CHECK_CHOICE", target = "CONFIRM_FORM")
    public void check2confirm(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---校验表单到待提交表单(choice true)---, form:[{}]", form);
    }

    //不会执行
    @OnTransition(source = "CHECK_CHOICE", target = "DEAL_FORM")
    public void check2deal(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---校验表单到待提交表单(choice false)---, form:[{}]", form);
    }

    @OnTransition(source = "DEAL_FORM", target = "DEAL_CHOICE")
    public void deal(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---处理复杂表单---, form:[{}]", form);
    }

    //不会执行
    @OnTransition(source = "DEAL_CHOICE", target = "FAILED_FORM")
    public void deal2fail(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---处理复杂表单失败(choice false)---, form:[{}]", form);
    }

    //不会执行
    @OnTransition(source = "DEAL_CHOICE", target = "FULL_FORM")
    public void deal2full(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---处理复杂表单到重新填写表单(choice true)---, form:[{}]", form);
    }

    /**
     * CONFIRM_FORM->SUCCESS_FORM 执行的动作
     */
    @OnTransition(source = "CONFIRM_FORM", target = "SUCCESS_FORM")
    public void submit(Message<ComplexEvent> message) {
        Form form = message.getHeaders().get(Form.class.getName(), Form.class);
        log.info("---表单提交成功---, form:[{}]", form);
    }


}
